package com.zw.cc.conf;

import com.zw.cc.redis.ShardedJedisSentinelPool;
import com.zw.cc.redis.ShardedJedisSentinelTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPoolConfig;

import java.util.List;
import java.util.Set;

@Configuration
public class RedisConfig {

    @Value("#{'${redis-config.masterNames}'.split(',')}")
    private List<String> masterNames;

    @Value("#{'${redis-config.sentinels}'.split(',')}")
    private Set<String> sentinels;

    @Bean
    @ConfigurationProperties(prefix = "redis.pool")
    public JedisPoolConfig jedisPoolConfig(){
        return new JedisPoolConfig();
    }

    @Bean
    public ShardedJedisSentinelPool shardedJedisSentinelPool(JedisPoolConfig jedisPoolConfig) {
        return new ShardedJedisSentinelPool(masterNames,sentinels,jedisPoolConfig);
    }

    @Bean
    public ShardedJedisSentinelTemplate redisTemplate(ShardedJedisSentinelPool shardedJedisSentinelPool) {
        return new ShardedJedisSentinelTemplate(shardedJedisSentinelPool);
    }
}
